package com.yuhanginfo.modules.web.web;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.modules.web.util.WebUserUtils;
import com.yuhanginfo.modules.web.entity.ShoppingCart;
import com.yuhanginfo.modules.web.entity.WebAddress;
import com.yuhanginfo.modules.web.entity.WebFreeWaybill;
import com.yuhanginfo.modules.web.entity.WebGoods;
import com.yuhanginfo.modules.web.entity.WebInvoiceInfo;
import com.yuhanginfo.modules.web.entity.WebOrders;
import com.yuhanginfo.modules.web.entity.WebOrdersDetail;
import com.yuhanginfo.modules.web.entity.WebStore;
import com.yuhanginfo.modules.web.entity.WebYongUser;
import com.yuhanginfo.modules.web.service.AddressService;
import com.yuhanginfo.modules.web.service.OrderService;
import com.yuhanginfo.modules.web.service.ShoppingCartService;
import com.yuhanginfo.modules.web.service.WebClassService;
import com.yuhanginfo.modules.web.service.WebFreeWaybillService;
import com.yuhanginfo.modules.web.service.WebGoodsService;
import com.yuhanginfo.modules.web.service.WebInvoiceInfoService;
import com.yuhanginfo.modules.web.service.WebOrdersDetailService;
import com.yuhanginfo.modules.web.service.WebStoreService;


/**
 * 购物车Controller
 * @author lvls
 * @version 2017-10-23
 */
@Controller
@RequestMapping(value = "${frontPath}/shoppingCart")
public class ShoppingCartController extends BaseController {

	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private WebOrdersDetailService webOrderDetailService;
	
	@Autowired
	private WebStoreService webStoreService;
	
	@Autowired
	private WebInvoiceInfoService webInvoiceInfoService;
	
	@Autowired
	private WebGoodsService webGoodsService;
	
	@Autowired
	private WebClassService webClassService;

	@Autowired
	private WebFreeWaybillService freeWaybillService;
	
	@ModelAttribute
	public ShoppingCart get(@RequestParam(required=false) String id) {
		ShoppingCart entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = shoppingCartService.get(id);
		}
		if (entity == null){
			entity = new ShoppingCart();
		}
		return entity;
	}

	@RequestMapping(value = "form")
	public String form(ShoppingCart shoppingCart, Model model) {
		model.addAttribute("shoppingCart", shoppingCart);
		return "modules/web/shoppingCartForm";
	}

	/**
	 * 立即购买
	 */
	@RequestMapping(value = "buyNow")
	public String buyNow(ShoppingCart shoppingCart,HttpServletRequest request, HttpServletResponse response, Model model) {
		//获取用户所有的收货地址
		WebYongUser webYongUser = WebUserUtils.getYongUser();
		if(webYongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		WebGoods buyGoods = webGoodsService.getByCode(shoppingCart.getGoodsCode());
		shoppingCart.setUserId(Integer.parseInt(webYongUser.getId()));
		ShoppingCart OldShoppingCart = shoppingCartService.selectByStoreCodeAndGoodsCodeAndUserId(shoppingCart);
		if(OldShoppingCart!=null){
			shoppingCart = OldShoppingCart;
		}else{
			shoppingCart.setGoodsImage(buyGoods.getImage());
			shoppingCart.setGoodsName(buyGoods.getCnName());
			shoppingCart.setGoodsNum(1);
			shoppingCart.setGoodsPrice(buyGoods.getPrice());
			shoppingCart.setStatus("1");
			shoppingCart.setUserId(Integer.parseInt(webYongUser.getId()));
			shoppingCart.setIsCheck("1");
			shoppingCart.setDelFlag("0");
		}
		shoppingCartService.save(shoppingCart);
		return "redirect:"+ frontPath +"/shoppingCart/goSettlement?storeCode="+shoppingCart.getStoreCode()+"&shoppingCartIds="+shoppingCart.getId(); 
	}
	
	/**
	 * 快速下单
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "quickOrder")
	public String quickOrder(String storeCode,HttpServletRequest request, HttpServletResponse response, Model model,HttpSession session,String ppType) {
		WebYongUser webYongUser = WebUserUtils.getYongUser();
		if(webYongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		
		List<WebGoods> list=(List<WebGoods>) session.getAttribute("orderList");

    	List<WebGoods> list0=new ArrayList<WebGoods>();
    	for (WebGoods webGoods : list) {
    		if("0".equals(webGoods.getFailReason())){
    			list0.add(webGoods);
    		}	
		}
    	String[] webGoodsIds=new String[list0.size()];
    	Integer[] webGoodsNums=new Integer[list0.size()];
    	for (int i = 0; i < list0.size(); i++) {
    		webGoodsIds[i]=list0.get(i).getId();
    		webGoodsNums[i]=list0.get(i).getGoodsNum();
		}
    	
		List<WebAddress> addressList = addressService.selectByUserId(Integer.parseInt(webYongUser.getId()));
		WebAddress defAddress = null;
		for (WebAddress address : addressList) {
			if("1".equals(address.getIsDef())){
				defAddress = address;
			}
		}
		WebStore webStore = webStoreService.getByCode(storeCode);
		model.addAttribute("addressList", addressList);
		model.addAttribute("webStore", webStore);
		model.addAttribute("webYongUser", webYongUser);
		model.addAttribute("defAddress", defAddress);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		model.addAttribute("list", list0);
		model.addAttribute("fastManId", webYongUser.getId());
		return "modules/web/Order/fastVipConfirmOrder";
	}
	
	@RequiresPermissions("web:shoppingCart:edit")
	@RequestMapping(value = "save")
	public String save(ShoppingCart shoppingCart, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shoppingCart)){
			return form(shoppingCart, model);
		}
		shoppingCartService.save(shoppingCart);
		addMessage(redirectAttributes, "保存购物车成功");
		return "redirect:"+Global.getAdminPath()+"/web/shoppingCart/?repage";
	}
	
	/**
	 * 添加商品到购物车
	 * @param 用户id userId
	 * @param 商品编号 goodsCode
	 */
	@RequestMapping(value = "addShoppingCart")
	@RequiresPermissions("user")
	@ResponseBody
	public Map<String,String> addShoppingCart(ShoppingCart shoppingCart) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		String goodsCode = shoppingCart.getGoodsCode();
		String storeCode = shoppingCart.getStoreCode();
		Map<String,String> map = new HashMap<String, String>();
		map.put("goodsCode", goodsCode);
		map.put("storeCode", storeCode);
		WebGoods WebGoods = webGoodsService.getByStoreCodeAndGoodsCode(map);
		Map<String,String> result = new HashMap<String, String>();
		if(WebGoods==null){
			result.put("status", "0");
		}else if(!(WebGoods.getStoreStock()>0)){
			result.put("status", "2");
		}else if(WebGoods.getStatus()!=1){
			result.put("status", "3");
		}else{
			result.put("status", "1");
		}
		shoppingCart.setUserId(Integer.parseInt(yongUser.getId()));
		shoppingCart.setStoreCode(shoppingCart.getStoreCode());
		ShoppingCart OldShoppingCart = shoppingCartService.selectByStoreCodeAndGoodsCodeAndUserId(shoppingCart);
		if(OldShoppingCart!=null){
			OldShoppingCart.setGoodsNum(OldShoppingCart.getGoodsNum()+1);
			shoppingCartService.save(OldShoppingCart);
		}else{
			shoppingCart.setGoodsNum(1);
			shoppingCart.setStatus("1");
			shoppingCart.setDelFlag("0");
			shoppingCart.setIsCheck("0");
			shoppingCartService.save(shoppingCart);
		}
		return result;
	}
	
	
	@RequestMapping(value = "delete")
	public String delete(ShoppingCart shoppingCart, RedirectAttributes redirectAttributes) {
		shoppingCartService.delete(shoppingCart);
		addMessage(redirectAttributes, "删除购物车成功");
		return "redirect:"+Global.getAdminPath()+"/web/shoppingCart/?repage";
	}
	
	@RequestMapping(value = "deleteGoods")
	@ResponseBody
	public String deleteGoods(ShoppingCart shoppingCart,String shoppingCartIds, RedirectAttributes redirectAttributes) {
		if(shoppingCartIds!=null){
			String[] split = shoppingCartIds.split(",");
			List<String> asList = Arrays.asList(split);
			for (String string : asList) {
				shoppingCart.setId(string);
				shoppingCartService.delete(shoppingCart);
			}
		}
		return "success";
	}
	
	/**
	 * 清空失效商品
	 */
	@RequestMapping(value = "deleteInvalidGoods")
	@ResponseBody
	public String deleteInvalidGoods(ShoppingCart shoppingCart, RedirectAttributes redirectAttributes) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		shoppingCart.setUserId(Integer.parseInt(yongUser.getId()));
		List<ShoppingCart> selectInvalidGoods = shoppingCartService.selectInvalidGoods(shoppingCart);
		for (ShoppingCart shoppingCart2 : selectInvalidGoods) {
			shoppingCartService.delete(shoppingCart2);
		}
		return "success";
	}
	
	/**
	 * 修改购物车商品数量
	 */
	@RequestMapping(value = "changeNum")
	@ResponseBody
	public String changeNum(ShoppingCart shoppingCart,RedirectAttributes redirectAttributes) {
		shoppingCartService.save(shoppingCart);
		return "success";
	}
	
	/**
	 * 提交订单时验证购物车中的商品是否有效
	 */
	@RequestMapping(value = "checkInvalid")
	@ResponseBody
	public String checkInvalid(String ids,RedirectAttributes redirectAttributes) {
		String[] split = ids.split(",");
		List<ShoppingCart> list = shoppingCartService.selectByids(split);
		for (ShoppingCart shoppingCart : list) {
			if("0".equals(shoppingCart.getStatus())){
				return "hasInvalid";
			}
		}
		return "noInvalid";
	}
	
	/**
	 * 修改购物车商品勾选状态
	 */
	@RequestMapping(value = "changeCheck")
	@ResponseBody
	public String changeCheck(ShoppingCart shoppingCart,String checkType, String idOrCode,String checkOrCancel,RedirectAttributes redirectAttributes) {
		List<ShoppingCart> list = new ArrayList<ShoppingCart>();
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if("all_list".equals(checkType)){//勾选全选
			list = shoppingCartService.selectByUserId(yongUser.getId());
		}else if("shop_list".equals(checkType)){//勾选门店
			list = shoppingCartService.selectByStoreCode(idOrCode);
		}else{
			list.add(shoppingCartService.get(idOrCode));
		}
		if(!list.isEmpty()){
			for (ShoppingCart shoppingCart2 : list) {
				if("check".equals(checkOrCancel)){
					shoppingCart2.setIsCheck("1");
				}else if("cancel".equals(checkOrCancel)){
					shoppingCart2.setIsCheck("0");
				}
				shoppingCartService.save(shoppingCart2);
			}
		}
		return "success";
		
	}
	
	@RequestMapping(value = {"selectGoodsByUserId",""})
	public String selectGoodsByUserId(ShoppingCart shoppingCart, HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		shoppingCart.setUserId(Integer.parseInt(yongUser.getId()));
		List<Map<String,Object>> list = shoppingCartService.selectGoodsByUserId(shoppingCart); 
		List<ShoppingCart> invalidList = shoppingCartService.selectInvalidGoods(shoppingCart); 
		model.addAttribute("yongUser", yongUser);
		model.addAttribute("list", list);
		model.addAttribute("invalidList", invalidList);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		return "modules/web/shoppingCart/shoppingCartList";
	}
	
	/**
	 * 跳转结算页面
	 */
	@RequestMapping(value = "goSettlement")
	public String goSettlement(ShoppingCart shoppingCart,String shoppingCartIds, HttpServletRequest request, HttpServletResponse response, Model model) {
		//获取用户所有的收货地址
		WebYongUser webYongUser = WebUserUtils.getYongUser();
		if(webYongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		String storeCode = shoppingCart.getStoreCode();
		WebStore webStore = webStoreService.getByCode(storeCode);
		List<WebAddress> addressList = addressService.selectByUserId(Integer.parseInt(webYongUser.getId()));
		WebAddress defAddress = null;
		for (WebAddress address : addressList) {
			if("1".equals(address.getIsDef())){
				defAddress = address;
			}
		}
		String[] split = shoppingCartIds.split(",");
		List<String> asList = Arrays.asList(split);
		shoppingCart.setUserId(Integer.parseInt(webYongUser.getId()));
		List<ShoppingCart> allShoppingCartList = shoppingCartService.findList(shoppingCart);
		List<ShoppingCart> showShoppingCartList = new ArrayList<ShoppingCart>();
		for (ShoppingCart shoppingCart2 : allShoppingCartList) {
			if(asList.contains(shoppingCart2.getId())){
				showShoppingCartList.add(shoppingCart2);
			}
		}
		if(showShoppingCartList.isEmpty()){
			return "redirect:"+ frontPath +"/webIndex";
		}
		model.addAttribute("addressList", addressList);
		model.addAttribute("webStore", webStore);
		model.addAttribute("webYongUser", webYongUser);
		model.addAttribute("defAddress", defAddress);
		model.addAttribute("showShoppingCartList", showShoppingCartList);
		model.addAttribute("allClassesList",webClassService.getAllClasses());
		if("1".equals(webYongUser.getUserType())){
			return "modules/web/shoppingCart/commonConfirmOrder";
		}else{
			return "modules/web/shoppingCart/vipConfirmOrder";
		}
	}
	
	
	/**
	 * 生成会员用户订单
	 */
	@RequestMapping(value = "vipConfirmOrder")
	public String vipConfirmOrder(WebOrders Order,String shoppingCartIds,String sendAreaId,String receiveAreaId, HttpServletRequest request, HttpServletResponse response, Model model) {
		if(WebUserUtils.getYongUser().getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		String[] shoppingCartIdArray = shoppingCartIds.split(",");
		List<ShoppingCart> list = shoppingCartService.selectByids(shoppingCartIdArray);
		if(list.isEmpty()){
			return "redirect:"+ frontPath +"/webIndex";
		}
		WebFreeWaybill freeWaybill = new WebFreeWaybill();
		freeWaybill.setReceiveAreaId(receiveAreaId);
		freeWaybill.setSendAreaId(sendAreaId);
		WebFreeWaybill webFreeWaybillByArea = freeWaybillService.getWebFreeWaybillByArea(freeWaybill);
		Order.setPayType("0");
		WebOrders insertOrder = insertOrder(Order,shoppingCartIds,webFreeWaybillByArea);
		return "redirect:"+ frontPath +"/payment/goPaymentShow?orderCode="+insertOrder.getCode();
	}

	/**
	 * 生成普通用户订单和发票信息
	 */
	@RequestMapping(value = "commonConfirmOrder")
	public String commonConfirmOrder(WebOrders Order,String shoppingCartIds,String sendAreaId,String receiveAreaId,WebInvoiceInfo webInvoiceInfo) {
		if(WebUserUtils.getYongUser().getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		String[] shoppingCartIdArray = shoppingCartIds.split(",");
		List<ShoppingCart> list = shoppingCartService.selectByids(shoppingCartIdArray);
		if(list.isEmpty()){
			return "redirect:"+ frontPath +"/webIndex";
		}
		WebFreeWaybill freeWaybill = new WebFreeWaybill();
		freeWaybill.setReceiveAreaId(receiveAreaId);
		freeWaybill.setSendAreaId(sendAreaId);
		WebFreeWaybill webFreeWaybillByArea = freeWaybillService.getWebFreeWaybillByArea(freeWaybill);
		Order.setPayType("1");
		WebOrders insertOrder = insertOrder(Order,shoppingCartIds,webFreeWaybillByArea);
		if("1".equals(Order.getIsInvoice())){
			webInvoiceInfo.setReceiveAddress(webInvoiceInfo.getInvoiceReceiveAddress());
			webInvoiceInfo.setReceiveName(webInvoiceInfo.getInvoiceReceiveName());
			webInvoiceInfo.setReceivePhone(webInvoiceInfo.getInvoiceReceivePhone());
			webInvoiceInfo.setMoney(insertOrder.getMoney());
			webInvoiceInfo.setOrderId(insertOrder.getId());
			webInvoiceInfo.setOrdersCode(insertOrder.getCode());
			webInvoiceInfo.setUserId(insertOrder.getUserId());
			webInvoiceInfo.setStatus(0);
			webInvoiceInfo.setPayType(1);
			if(webInvoiceInfo.getInvoiceType()==1){
				webInvoiceInfo.setInvoiceTitle(webInvoiceInfo.getCompany());
			}
			webInvoiceInfoService.save(webInvoiceInfo);
		}
		return "redirect:"+ frontPath +"/payment/goPaymentShow?orderCode="+Order.getCode();
	}
	
	/**
	 * 快速下单提交
	 */
	@RequestMapping(value = "quickOrderConfirm")
	public String quickOrderConfirm(WebOrders Order,
			String[] goodsCode,
			Integer[] num,
			String sendAreaId,
			String receiveAreaId) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		if(yongUser.getId()==null){
			return "modules/web/webLogin";//到登陆页面
		}
		BigDecimal money = BigDecimal.valueOf(0, 2);
		BigDecimal payMoney1 = BigDecimal.valueOf(0, 2);
		
		for (int i=0;i<goodsCode.length;i++) {
			String code = goodsCode[i];
			//根据购物车中勾选的商品数量和单价计算订单总价、运费和支付金额
			WebGoods WebGoods = webGoodsService.getByCode(code);
			BigDecimal goodsPrice1 = BigDecimal.valueOf(0, 2);
			Integer num1 = num[i];
			BigDecimal goodsNum = BigDecimal.valueOf(num1);
			if("2".equals(yongUser.getUserType())){
				goodsPrice1 = WebGoods.getVipPrice();
			}else{
				goodsPrice1 = WebGoods.getPrice();
			}
			money = money.add(goodsPrice1.multiply(goodsNum));
		}
		WebFreeWaybill freeWaybill = new WebFreeWaybill();
		freeWaybill.setReceiveAreaId(receiveAreaId);
		freeWaybill.setSendAreaId(sendAreaId);
		WebFreeWaybill webFreeWaybillByArea = freeWaybillService.getWebFreeWaybillByArea(freeWaybill);
		if("1".equals(Order.getTransportType())){//选择物流
			if(webFreeWaybillByArea!=null){
				//价格低于包邮价格
				if(money.compareTo(webFreeWaybillByArea.getMoney())==-1){
					//运费
					BigDecimal tranfee = money.multiply(BigDecimal.valueOf(0.1));
					Order.setFreight(tranfee);
					payMoney1 = money.add(tranfee);
				}else{
					payMoney1 = money;
				}
			}else{
				//运费
				BigDecimal tranfee = money.multiply(BigDecimal.valueOf(0.1));
				Order.setFreight(tranfee);
				payMoney1 = money.add(tranfee);
			}
		}else{//选择快递
			payMoney1 = money;
		}
		if(yongUser.getSysUserId()!=null){
			Order.setSysUserId(yongUser.getSysUserId());
		}
		Order.setMoney(money);
		Order.setPayMoney(payMoney1);
		Order.setUserId(Integer.parseInt(yongUser.getId()));
		String orderCode = getOrderReturnCode();
		Order.setCode(orderCode);
		Order.setIsHistory("0");
		Order.setState("1");
		Order.setOrderType(1);
		for (int i=0;i<goodsCode.length;i++) {
			//新增订单详情
			WebOrdersDetail webOrderDetail = new WebOrdersDetail();
			WebGoods byCode = webGoodsService.getByCode(goodsCode[i]);
			webOrderDetail.setOrdersCode(orderCode);
			webOrderDetail.setPartsCode(byCode.getPartCode());
			webOrderDetail.setGoodsCode(byCode.getGoodsCode());
			webOrderDetail.setGoodsCnName(byCode.getCnName());
			webOrderDetail.setNum(byCode.getGoodsNum());
			if("1".equals(yongUser.getUserType())){
				webOrderDetail.setPrice(byCode.getPrice());
			}else{
				webOrderDetail.setPrice(byCode.getVipPrice());
			}
			webOrderDetail.setIsHistroy("0");
			webOrderDetailService.save(webOrderDetail);
		}
		orderService.save(Order);
		return "redirect:"+ frontPath +"/payment/goPaymentShow?orderCode="+Order.getCode();
	}
	
	@RequestMapping(value = "getShoppingCartNum")
	@ResponseBody
	public List<ShoppingCart> getShoppingCartNum(HttpServletRequest request, HttpServletResponse response, Model model) {
		WebYongUser yongUser = WebUserUtils.getYongUser();
		return shoppingCartService.selectByUserId(yongUser.getId());
	}
	
	
	
	
	/****************************************内部方法*********************************************/
	public String getOrderReturnCode(){
		String format = "%02d";
		Date nowDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String now = sdf.format(nowDate);
		int OrderCode = getMaxOrderReturnCode(now);
		OrderCode = OrderCode+1;
		String a = now+format;
		return String.format(a, OrderCode);
	}
	
	private int getMaxOrderReturnCode(String now){
		String code = orderService.findMaxCode(now);
		if(code!=null){
			return Integer.parseInt(code.substring(14));
		}else{
			return 0;
		}
	}
	
	/**
	 * 根据接收到的订单信息和商品codes生成订单和订单详情
	 */
	public WebOrders insertOrder(WebOrders Order,String shoppingCartIds,WebFreeWaybill webFreeWaybillByArea){
		WebYongUser yongUser = WebUserUtils.getYongUser();
		Integer userId = Integer.parseInt(yongUser.getId());
		String[] shoppingCartIdArray = shoppingCartIds.split(",");
		List<ShoppingCart> list = shoppingCartService.selectByids(shoppingCartIdArray);
		BigDecimal money = BigDecimal.valueOf(0, 2);
		BigDecimal payMoney = BigDecimal.valueOf(0, 2);
		for (ShoppingCart ShoppingCart : list) {
			//根据购物车中勾选的商品数量和单价计算订单总价、运费和支付金额
			BigDecimal goodsPrice = BigDecimal.valueOf(0, 2);
			Integer goodsNum = ShoppingCart.getGoodsNum();
			BigDecimal num = BigDecimal.valueOf(goodsNum);
			if("2".equals(yongUser.getUserType())){
				goodsPrice = ShoppingCart.getVipPrice();
			}else{
				goodsPrice = ShoppingCart.getGoodsPrice();
			}
			money = money.add(goodsPrice.multiply(num));
		}
		BigDecimal tranfee = BigDecimal.valueOf(0.00);
		if("1".equals(Order.getTransportType())){//选择物流
			if(webFreeWaybillByArea!=null){
				//价格低于包邮价格
				if(money.compareTo(webFreeWaybillByArea.getMoney())==-1){
					//运费
					tranfee = money.multiply(BigDecimal.valueOf(0.1));
					payMoney = money.add(tranfee);
				}else{
					payMoney = money;
				}
			}else{
				//运费
				tranfee = money.multiply(BigDecimal.valueOf(0.1));
				Order.setFreight(tranfee);
				payMoney = money.add(tranfee);
			}
		}else{//选择快递
			payMoney = money;
		}
		Order.setFreight(tranfee);
		if(yongUser.getSysUserId()!=null){
			Order.setSysUserId(yongUser.getSysUserId());
		}
		Order.setMoney(money);
		Order.setPayMoney(payMoney);
		Order.setUserId(userId);
		String orderCode = getOrderReturnCode();
		Order.setCode(orderCode);
		Order.setIsHistory("0");
		Order.setState("1");
		Order.setOrderType(1);
		for (ShoppingCart shoppingCart : list) {
			//新增订单详情
			WebOrdersDetail webOrderDetail = new WebOrdersDetail();
			WebGoods byCode = webGoodsService.getByCode(shoppingCart.getGoodsCode());
			webOrderDetail.setOrdersCode(orderCode);
			webOrderDetail.setPartsCode(byCode.getPartCode());
			webOrderDetail.setGoodsCode(shoppingCart.getGoodsCode());
			webOrderDetail.setGoodsCnName(shoppingCart.getGoodsName());
			webOrderDetail.setGoodsEnName(shoppingCart.getGoodsEnName());
			webOrderDetail.setNum(shoppingCart.getGoodsNum());
			if("1".equals(yongUser.getUserType())){
				webOrderDetail.setPrice(shoppingCart.getGoodsPrice());
			}else{
				webOrderDetail.setPrice(shoppingCart.getVipPrice());
			}
			webOrderDetail.setIsHistroy("0");
			webOrderDetailService.save(webOrderDetail);
			shoppingCartService.delete(shoppingCart);
		}
		orderService.save(Order);
		return orderService.selectWebOrdersByCode(Order.getCode());
	}
}